import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { OrderService } from '../services/order.service';
import { Order } from '../models/order.entity';

@Controller('orders')
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @Get()
  async getOrders(): Promise<Order[]> {
    return this.orderService.findAll();
  }

  @Get(':id')
  async getOrder(@Param('id') id: string): Promise<Order | null> {
    return this.orderService.findOne(Number(id));
  }

  @Post()
  @UsePipes(new ValidationPipe())
  async createOrder(@Body() orderData: Partial<Order>): Promise<Order> {
    return this.orderService.create(orderData);
  }

  @Put(':id')
  @UsePipes(new ValidationPipe())
  async updateOrder(
    @Param('id') id: string,
    @Body() orderData: Partial<Order>,
  ): Promise<Order | null> {
    return this.orderService.update(Number(id), orderData);
  }

  @Delete(':id')
  async deleteOrder(@Param('id') id: string): Promise<Order | null> {
    return this.orderService.remove(Number(id));
  }
}
